﻿
--工作单位异动申请
CREATE proc [dbo].[proc_Employee_CompanyMoveApply]
(
	@eid int,
	@ACompanyId int,
	@ADepartmentId int,
	@AZhiWu nvarchar(50),
	@reason text,
	@proposer nvarchar(50),
	@id int,
	@kbn INT,
	@execdate DATETIME,
	@APid INT,
	@ACid INT
)
As
Begin
	If @ACompanyId Is Null Or @ADepartmentId Is Null
	Begin
		Select 0 As ReturnValue
		Return
	End

	Declare @PostMove_Id int
	Set @PostMove_Id = Null
	Select Top 1 @PostMove_Id=id From Employee_CompanyMove Where Audit=0 And Eid = @eid And AuditPerson Is Null
	If @PostMove_Id Is Not Null	--该人有尚未审批的申请，退出
	Begin
		Select -1 As ReturnValue
		Return
	End

	Declare @BCompanyId int, @BDepartmentId int,@BZhiWu nvarchar(50),@EName nvarchar(50),@BPid INT,@BCid INT,@PostId int,@PostName nvarchar(50),@GradeId int,@GradeName nvarchar(50),@XZJB nvarchar(50),@BCompanyName nvarchar(200),@ACompanyName nvarchar(200),@BDepartmentName nvarchar(200),@ADepartmentName nvarchar(200)
	Select @BCompanyId=CompanyId,@BDepartmentId=DepartmentId,@BZhiWu=ZhiWu,@EName=EName,@BPid = Pid,@BCid = Cid,
	@PostId=Postid,@GradeId=Gradeid,@PostName=(select top 1 P.PostName from Post P where P.id=Employee.PostId),
	@GradeName=(select Title from Grade  where id=GradeId) ,@XZJB=XZJB 	
	From Employee Where id = @eid
	
	select  @BCompanyName=companyname from Company where id=@BCompanyId
	select  @ACompanyName=companyname from Company where id=@ACompanyId
	select @BDepartmentName=dname from department where id=@BDepartmentId
	select @ADepartmentName=dname from department where id=@ADepartmentId
	
	insert into employee_CompanyMove
	(
		eid,BCompanyId,ACompanyId,BDepartmentId,ADepartmentId,BZhiWu,AZhiWu,reason,proposer,appliontime,EName,
		Execdate,BPid,BCid,APid,ACid,BCompanyName,ACompanyName,BDepartmentName,ADepartmentName,PostName,GradeName,XZJB,
		Gradeid,Postid
	) 
	values
	(
		@eid,@BCompanyId,@ACompanyId,@BDepartmentId,@ADepartmentId,@BZhiWu,@AZhiWu,@reason,@proposer,getdate(),@EName,
		@execdate,@BPid,@BCid,@APid,@ACid,@BCompanyName,@ACompanyName,@BDepartmentName,@ADepartmentName,@PostName,@GradeName,@XZJB,
		@GradeId,@PostId
	)

	Select @@ROWCOUNT As ReturnValue
	Return
End